package it.univaq.mwt.client;

import it.univaq.mwt.tp.Customer;
import it.univaq.mwt.tp.Department;
import it.univaq.mwt.tp.Employee;
import it.univaq.mwt.tp.Item;
import it.univaq.mwt.tp.ItemDesc;
import it.univaq.mwt.tp.Order;
import it.univaq.mwt.tp.OrderLine;

import java.util.Iterator;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Populate {

    Department depSales;
    Department depAdmin;
    Customer c1;
    Customer c2;
    Order o1;
    Order o2;

    public void populate(EntityManager em) {

        System.out.println ("Transaction begins.");
        em.getTransaction().begin();

        System.out.println("Creating Employees, Departments, Customers...");

        depSales = new Department("Sales");

        Employee emp = new Employee(1, "AAAA1", "John Brown");
        depSales.addEmployee(emp);
        emp.setDepartment(depSales);
        emp.setSalary((float)1000.0);

        emp = new Employee(2, "AAAA2", "Paul Smith");
        depSales.addEmployee(emp);
        emp.setDepartment(depSales);
        emp.setSalary((float)1500.0);

        emp = new Employee(3, "AAAA3", "Marc Black");
        depSales.addEmployee(emp);
        emp.setDepartment(depSales);
        emp.setSalary((float)1000.0);

        depAdmin = new Department("Administration");

        emp = new Employee(4, "AAAA4", "Jim Doe");
        depAdmin.addEmployee(emp);
        emp.setDepartment(depAdmin);
        emp.setSalary((float)2000.0);

        emp = new Employee(5, "AAAA5", "Bob Lester");
        depAdmin.addEmployee(emp);
        emp.setDepartment(depAdmin);
        emp.setSalary((float)2000.0);

        em.persist(depSales);
        em.persist(depAdmin);

        c1 = new Customer(1, "AAAAA6", "Peter Lamb");
        c2 = new Customer(2, "AAAAA7", "David Biggs");


        System.out.println("Creating Items inventory...");

        ItemDesc desc = new ItemDesc("Black Jacket");
        Item i1 = new Item(1, (float)200.0, 20,  desc);
        desc = new ItemDesc("Red Tie");
        Item i2 = new Item(2, (float)200.0, 30,  desc);

        em.persist(i1);
        em.persist(i2);
        
        System.out.println("Creating Orders...");
        o1 = new Order(1);
        o1.setCustomer(c1);
        OrderLine oli = null;
        try {
            oli = new OrderLine(i1);
            oli.setQuantity(1);
            o1.addOrderLine(oli);
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        try {
            oli = new OrderLine(i2);
            oli.setQuantity(2);
            o1.addOrderLine(oli);
        } catch ( Exception e ) {
            e.printStackTrace();
        }

        o2 = new Order(2);
        o2.setCustomer(c2);
        try {
            oli = new OrderLine(i2);
            oli.setQuantity(3);
            o2.addOrderLine(oli);
        } catch ( Exception e ) {
            e.printStackTrace();
        }

        em.persist(o1);
        em.persist(o2);
        em.getTransaction().commit();
        System.out.println ("Transaction complete.");
    }

    public void list() {
        System.out.println("Listing objects...");
        System.out.println("#### Employees:");
        System.out.println("     "+depSales.getName());
        Iterator iterator = depSales.getEmployees().iterator();
        while (iterator.hasNext()) {
            Employee e = (Employee)iterator.next();
            System.out.println("        "+e.getEmpID()+"  "+e.getSsn()+"  "+e.getName()+"  "+e.getSalary());
        }
        System.out.println("     "+depAdmin.getName());
        iterator = depAdmin.getEmployees().iterator();
        while (iterator.hasNext()) {
            Employee e = (Employee)iterator.next();
            System.out.println("        "+e.getEmpID()+"  "+e.getSsn()+"  "+e.getName()+"  "+e.getSalary());
        }
        System.out.println("#### Orders:");
        System.out.println("     "+o1.getOrderID()+"  " +o1.getCustomer().getName()+" "+o1.getOrderDate());
        iterator = o1.getOrderLines().iterator();
        while (iterator.hasNext()) {
            OrderLine oli = (OrderLine)iterator.next();
            System.out.println("           "+oli.getItem().getItemDesc().getDescription()+"  "+oli.getQuantity()+"  "+oli.getLineTotal());
        }
        System.out.println("     "+o2.getOrderID()+"  " +o2.getCustomer().getName()+" "+o2.getOrderDate());
        iterator = o2.getOrderLines().iterator();
        while (iterator.hasNext()) {
            OrderLine oli = (OrderLine)iterator.next();
            System.out.println("           "+oli.getItem().getItemDesc().getDescription()+"  "+oli.getQuantity()+"  "+oli.getLineTotal());
        }
    }


    public static void main(String[] argv) {

    	EntityManagerFactory emf = Persistence.createEntityManagerFactory("TPLabJPA");
    	EntityManager em = emf.createEntityManager();
        Populate p = new Populate();
        System.out.println("Populating domain model...");
        
		p.populate(em);
        p.list();

        em.close();
        emf.close();
        
    }

}
